
//STATUS
var status_loading = false;

var data = null;
//action message
var act_crawler_get_url = "/api_crawler/geturl";


function main_post(url,data,function_handle){
	if($("#loading").length <= 0){
		loading = $('<div id="loading"></div>');
		loading.hide()
	    .css({position:'absolute', zIndex:'5','top':'0','right':'300px'})
	    .appendTo('body')
	    .html("loading.....");
	}

	if(status_loading == false){
		status_loading = true;
		loading.show();
		$.post(ROOT_URL+url, data,function_handle).done(function(e){
			loading.hide();
			status_loading = false;
		});
	}	
}

function crawler_get_url(id){
	var data = {'id':id};
	main_post(act_crawler_get_url, data, do_not);
}



/*=========================
 * AUTO CRAWLER
 * From View: api/crawler/auto
 ==========================*/
function auto_crawler(){
	var act_crawler_websites = "/api_crawler/auto";
	data = {};
	main_post(act_crawler_websites, data,show_response);
}



/*=========================
 * CRAWLER
 * From View: api/crawler/*
 ==========================*/
function menu_handle(id,menu_type){
	if(menu_type == 'update_ranking_kw'){
		crawler_update_kw_ranking(id);
	}else if(menu_type=='update_ranking_website'){
		crawler_update_website_ranking(id);
	}else if(menu_type == 'update_ranking_cate_website'){
		crawler_update_category_website_ranking(id);
	}
}

function crawler_view_websites(crawler_id){
	var act_crawler_websites = "/api_crawler/websites";
	data = {'id':crawler_id};
	main_post(act_crawler_websites, data,show_response);
}

function crawler_get_url(crawler_id){
	var act_crawler_get_url = "/api_crawler/geturl";
	data = {'id':crawler_id};
	main_post(act_crawler_get_url, data,show_response);
}

function crawler_get_kw(crawler_id){
	var act_crawler_get_url = "/api_crawler/getkw";
	data = {'id':crawler_id};
	main_post(act_crawler_get_url, data,show_response);
}

function crawler_update_kw_ranking(crawler_id){
	var act_update_kw_ranking = "/api_crawler/updatekwranking";
	data = {'id':crawler_id};
	main_post(act_update_kw_ranking, data,show_response);
}

function crawler_update_website_ranking(crawler_id){
	var act_update_kw_ranking = "/api_crawler/updatewebsiteranking";
	data = {'id':crawler_id};
	main_post(act_update_kw_ranking, data,show_response);
}

function crawler_update_category_website_ranking(crawler_id){
	var act_update_kw_ranking = "/api_crawler/updatecategorywebsiteranking";
	data = {'id':crawler_id};
	main_post(act_update_kw_ranking, data,show_response);
}
////////////////////////////////////////

/*=========================
 * WEBSITE RANKING
 * From View: api/crawler/*
 ==========================*/
function keyword_ranking_menu_handle(id,menu_type){
	var arr_id = id.split('-');
	var history_id = arr_id[0];
	var keyword_id = arr_id[1];
	
	if(menu_type == 'page'){
		var url = ROOT_URL + "/ranking/page?history_id="+ history_id +"&keyword_id=" + keyword_id;
		window.open(url,'_blank');
	}else if(menu_type == 'website'){
		var url = ROOT_URL + "/ranking/website?history_id="+ history_id +"&keyword_id=" + keyword_id;
		window.open(url,'_blank');
	}
	
}


////////////////////////////////////////


/*=========================
 * WEBSITE RANKING
 * From View: api/crawler/*
 ==========================*/
function website_ranking_menu_handle(id,menu_type){
	var arr_id = id.split('-');
	var history_id = arr_id[0];
	var keyword_id = arr_id[1];
	var website_id = arr_id[2];
	
	if(menu_type == 'page'){
		var url = ROOT_URL + "/ranking/website_page?history_id="+ history_id +"&keyword_id=" + keyword_id+"&website_id="+website_id;
		window.open(url,'_blank');
		//$.facebox('Chức năng này hiện tại đang xây dựng...');
	}else if(menu_type == 'keyword'){
		view_website_keyword_ranking(id);
	}
}

function view_website_keyword_ranking(id){
	var act_update_kw_ranking = "/api_ranking_website/viewkw";
	data = {'id':id};// website ranking id
	main_post(act_update_kw_ranking, data,show_response);
}

/*=========================
 * KEYWORD RANKING
 * From View: ranking/keyword
 ==========================*/
function load_ranking(history_id){
	var url= "/ranking/load_keyword_ranking";
	var data = {'history_id':history_id};
	$.get(ROOT_URL+url, data,function(res){
		$("#ranking_detail").html(res);
	});
}

/*=========================
 * COMMON
 ==========================*/
function do_not(){
	//ko thuc hien j het
}

function show_response(data){
	$.facebox(data);
}